package simpleGui;

import java.awt.event.MouseEvent;
import java.util.Vector;

import processing.core.PGraphics;

public class GuiScrollList extends GuiComponent {
float modelHeight;
int tY;

int scrollBarHalfHeight;
int s1,s2;
int sDelta;
int contentDelta;
int barWidth=10;
PGraphics canvas;
int hContent;
boolean scroll;
public Vector<GuiCheckButton>buttonList=new Vector<GuiCheckButton>();
Vector<Integer>initialY=new Vector<Integer>();


public GuiScrollList(Gui gui, int x, int y, int w, int h, int hContent){
	super(gui,x,y,w,h);
	this.hContent=hContent;
	scrollBarHalfHeight=(int)(((h*h)/hContent)*0.5f);
	s1=y+scrollBarHalfHeight;
	s2=y+h-scrollBarHalfHeight;
	tY=s1;
	sDelta=s2-s1;
	contentDelta=hContent-h;
	canvas=gui.parent.createGraphics(w-barWidth,hContent);
	
}
public void setList(Vector<String>entries){
	int bH=20;
	buttonList=new Vector<GuiCheckButton>();
	initialY=new Vector<Integer>();
	this.hContent=Math.max(entries.size()*bH,h);
	if (hContent>h)scroll=true;
	else scroll=false;
	scrollBarHalfHeight=(int)(h*(h*1f/hContent)*0.5f);
	System.out.println("scrollBarHalfHeight: "+scrollBarHalfHeight);
	s1=y+scrollBarHalfHeight;
	s2=y+h-scrollBarHalfHeight;
	tY=s1;
	sDelta=s2-s1;
	contentDelta=hContent-h;
	canvas=gui.parent.createGraphics(w-barWidth,hContent);
	
	for (int i=0;i<entries.size();i++){
		GuiCheckButton button=new GuiCheckButton(gui,x+barWidth,y+i*bH,w-barWidth,bH,false);
		initialY.add(button.y);
		button.label=entries.get(i);
		buttonList.add(button);
		this.children.add(button);
		gui.parent.removeMouseListener(button);
	}
}
int transY;
public void display(PGraphics g){
	 transY =0;
	if (scroll){
	 transY=(int)(-contentDelta*(tY-s1)/(s2-s1));
	}
	for (int i=0;i<buttonList.size();i++){
		GuiCheckButton button=buttonList.get(i);
		button.y=initialY.get(i)+transY;
	}
	
	g.pushStyle();
	g.strokeWeight(gui.getStrokeWeight());
	g.noStroke();
	g.fill(gui.getFillColor());
	g.rectMode(PGraphics.CORNER);
	g.rect(x,y,w,h);
	
	canvas.beginDraw();
	canvas.translate(-(x+barWidth),-y);
	canvas.background(gui.getFillColor());
	this.displayChildren(canvas);
	canvas.endDraw();
	g.image(canvas,x+barWidth,y);
	g.noFill();
	g.stroke(gui.getStrokeColor());
	g.rect(x,y,x+barWidth,h);
	g.rect(x+barWidth,y,x+w-barWidth,h);
	g.fill(gui.getFillColorActivated());
	g.rect(x,tY-scrollBarHalfHeight,barWidth,scrollBarHalfHeight*2);
	g.popStyle();
	
}

boolean dragMode=false;

@Override
public void mousePressed(MouseEvent arg0) {
	// TODO Auto-generated method stub
	super.mousePressed(arg0);
	int mx=arg0.getX();
	int my=arg0.getY();
	if (this.contains(mx, my)){
		
		if (mx<x+barWidth){
			if (my>tY-scrollBarHalfHeight&&my<tY+scrollBarHalfHeight){
				System.out.println("inside");
				dragMode=true;
			}
		}
		else{
		// find the right bottom.
		int cY=arg0.getY()-transY;
		int iButton=(cY-y)/20;
		buttonList.get(iButton).flipState();
		}
		// swap.
		
	}
}



@Override
public void mouseReleased(MouseEvent arg0) {
	// TODO Auto-generated method stub
	
	dragMode=false;
	super.mouseReleased(arg0);
}

public void mouseDragged(MouseEvent arg0) {
	// TODO Auto-generated method stub
	if (dragMode){
		tY=arg0.getY();
		tY=Math.max(s1, tY);
		tY=Math.min(s2, tY);
		System.out.println("tY"+tY);
	}
	super.mouseMoved(arg0);
}

}
